using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Hardware;

namespace MicroSnake
{
    class OptionScreen : Screen
    {
        public OptionScreen()
        {
            InitializeComponent();
        }

        private Text txtTitle;
        private ListBox menuList;
        private ListBoxTextMenuItem difficultyMenuItem;
        private ListBoxTextMenuItem languageMenuItem;
        private ListBoxTextMenuItem backMenuItem;
        
        /// <summary>
        /// Build the menu graphical user interface
        /// </summary>
        private void InitializeComponent()
        {
            menuList = new ListBox();
            menuList.HorizontalAlignment = HorizontalAlignment.Center;

            // Menu Title                
            txtTitle = new Text()
            {
                Font = Resources.GetFont(Resources.FontResources.NinaB),
                ForeColor = MicroSnakeColors.MenuTitleForeColor,
                HorizontalAlignment = HorizontalAlignment.Center,
                TextAlignment = TextAlignment.Center,
                TextContent = Resources.GetString(Resources.StringResources.OptionsTitle),
                VerticalAlignment = VerticalAlignment.Center,
            };
            txtTitle.SetMargin(10);            

            menuList.Items.Add(new ListBoxMenuTitle(txtTitle));

            // Separator
            menuList.Items.Add(new ListBoxSpaceItem());

            // Menu Items
            difficultyMenuItem = ListBoxTextMenuItem.Make(Resources.StringResources.OptionsScreenDifficulty);
            languageMenuItem = ListBoxTextMenuItem.Make(Resources.StringResources.OptionsScreenLanguage);
            backMenuItem = ListBoxTextMenuItem.Make(Resources.StringResources.OptionsScreenBackToMainMenu);

            menuList.Items.Add(difficultyMenuItem);
            menuList.Items.Add(languageMenuItem);
            menuList.Items.Add(backMenuItem);

            menuList.SelectedItem = difficultyMenuItem;

            // Add event handler
            menuList.AddHandler(Buttons.ButtonDownEvent, new ButtonEventHandler(menuList_ButtonDown), false);

            this.Children.Add(menuList);
        }

        /*********************************************************************/
        /*                       Event Handling                              */
        /*********************************************************************/

        void menuList_ButtonDown(object sender, ButtonEventArgs e)
        {
            if (e.Button == Button.VK_SELECT)
            {
                if (menuList.SelectedItem == difficultyMenuItem)
                {
                    ScreenManager.Instance.ShowDifficultyLevelScreen();
                }
                else if (menuList.SelectedItem == languageMenuItem)
                {
                    ScreenManager.Instance.ShowLanguageScreen();
                }
                else if (menuList.SelectedItem == backMenuItem)
                {
                    ScreenManager.Instance.ShowMenuScreen();
                }
            }
        }

        /// <summary>
        /// Set the focus to the internal menuList
        /// </summary>
        public override void Focus()
        {
            menuList.SelectedItem = difficultyMenuItem;
            Buttons.Focus(menuList);
        }
    }
}
